/**
 * @Author: 陈瑞鹏
 * @Date:   2023-01-29 10:11:19
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-02-01 11:47:03
 */
import bagCtr from "../ctrs/bags/bagCtr";
import netCtr from "../ctrs/netCtr";
import BaseGameComponent from "../libs/core/BaseGameComponent";
import BaseGameLayer from "../libs/core/BaseGameLayer";
import { PureMVCEvents } from "../libs/core/PureMVCEvents";
import { drawScroll2 } from "../libs/core/utils";
import { INotification } from "../libs/pureMvc/Interfaces";

const { ccclass, property } = cc._decorator;
@ccclass
export default class bagLayer extends BaseGameLayer {

  @property({ type: cc.Node, tooltip: "内容节点" })
  contNode: cc.Node = null;

  @property({ type: cc.Prefab, tooltip: "预制体" })
  bagPref: cc.Prefab = null;

  @property({ type: cc.Label, tooltip: "道具描述" })
  djDesc: cc.Label = null;

  public onEnable(): void {
    super.onEnable();
    this.drawUI();
  }

  drawUI() {
    let list = bagCtr.ins.bagDic.getValueList();
    // console.log(list);
    this.contNode.removeAllChildren();
    drawScroll2(this.contNode, list, this.bagPref, "bagItemCom", 5, 20, 10);
  }

  public listNotificationInterests(): string[] {
    return [PureMVCEvents.ON_BAGCOM_UDPATE];
  }

  public handleNotification(notification: INotification): void {
    let evtName = notification.getName();
    let body = notification.getBody();
    switch (evtName) {
      case PureMVCEvents.ON_BAGCOM_UDPATE:
        this.drawUI();
        if (bagCtr.ins.curEny) {
          this.djDesc.string = `道具描述:${bagCtr.ins.curEny.desc}\n修补花费:${bagCtr.ins.curEny.cost}钻石`;
        } else {
          this.djDesc.string = ``;
        }
        break;
    }
  }

  onClick() {
    if (bagCtr.ins.curEny) {
      netCtr.ins.bagUpdate(bagCtr.ins.curEny.id);
    }
  }
}